package com.uniqueyi.mh;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private EditText et_a;
    private EditText et_p;
    private EditText et_dec;
    private Button bt_c;
    private TextView tv_result;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initEvent();
    }

    private void initEvent() {
        bt_c.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isEmpyt()) {
                    Toast.makeText(MainActivity.this, "请输入A和P的值", Toast.LENGTH_SHORT).show();
                    return;
                }
                String ss = getString(rectP());
                String ss2 = result();
                tv_result.setText("rectP:\n" + ss + "\nresult:\n" + ss2);
            }
        });
    }

    private void initView() {
        et_a = findViewById(R.id.et_a);
        et_p = findViewById(R.id.et_p);
        bt_c = findViewById(R.id.bt_c);
        et_dec = findViewById(R.id.et_dec);
        tv_result = findViewById(R.id.tv_result);
    }

    private boolean isEmpyt() {
        if (TextUtils.isEmpty(et_a.getText()) || TextUtils.isEmpty(et_p.getText())) {
            return true;
        } else {
            return false;
        }
    }


    private String[] split(String s) {
        return s.split(",");
    }

    /**
     * 解析A
     *
     * @param a
     * @return
     */
    private Float[] analysisA(String a) {
        String[] A = split(a);
        Float[] Af = new Float[A.length];
        for (int i = 0; i < A.length; i++) {
            Af[i] = Float.parseFloat(A[i]);
        }
        return Af;
    }

    /**
     * 解析矩阵P
     *
     * @param a
     * @return
     */
    private Float[][] analysisP(String a) {
        String[] P = a.split("\n");
        Float[][] Pf = new Float[P.length][split(P[0]).length];
        for (int i = 0; i < P.length; i++) {
            String[] P2 = split(P[i]);
            for (int j = 0; j < P2.length; j++) {
                if (TextUtils.isEmpty(P2[j])) {
                    Pf[i][j] = Float.valueOf(0);
                } else {
                    Pf[i][j] = Float.parseFloat(P2[j]);
                }
            }
        }
        return Pf;
    }


    /**
     * 计算矩阵P
     *
     * @return
     */
    private Float[] rectP() {
        Float[] a = analysisA(et_a.getText().toString());
        Float[][] p = analysisP(et_p.getText().toString());
        if (a.length != p.length) {
            Toast.makeText(this, "a个数要与p行数相等", Toast.LENGTH_SHORT).show();
            return null;
        }
        int length = p[0].length;
        Float[] te = new Float[length];
        List<Float> temp = new ArrayList<>();
        for (int j = 0; j < length; j++) {
            temp.clear();
            for (int i = 0; i < a.length; i++) {
                temp.add(getMin(p[i][j], a[i]));
            }
            Float Max = Collections.max(temp);
            te[j] = Max;
        }
        return te;
    }

    @NonNull
    private String getString(Float[] te) {
        if (te == null) {
            return "";
        }
        String temp = "(";
        for (int i = 0; i < te.length; i++) {
            temp += te[i] + ",";
        }
        return temp.substring(0, temp.length() - 1) + ")";
    }

    /**
     * 获取最终结果
     *
     * @return
     */
    private String result() {
        Integer d = null;
        try {
            d = Integer.parseInt(et_dec.getText().toString());
        } catch (Exception e) {
        }
        Float[] p = rectP();
        if (p == null) {
            return "";
        }
        Float[] res = new Float[p.length];
        Float de = getNum(p);
        for (int i = 0; i < p.length; i++) {
            if (d == null) {
                res[i] = (p[i] / de);
            } else {
                res[i] = (new BigDecimal(p[i] / de).setScale(d, BigDecimal.ROUND_HALF_UP).floatValue());
            }
        }
        return getString(res);
    }

    private Float getNum(Float[] p) {
        Float num = Float.valueOf(0);
        for (int i = 0; i < p.length; i++) {
            num += p[i];
        }
        return num;
    }

    /**
     * 获取最小值
     *
     * @param a
     * @param b
     * @return
     */
    private Float getMin(Float a, Float b) {
        return a > b ? b : a;
    }
}
